package com.yu.autobean01;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
 
@Component
//@PropertySource(value = "classpath:/com/lv/app.properties")
public class Gong {

	@Autowired
	Environment environment;
	//可调用方法很多，自行探索
	private void testE() {
		String property = environment.getProperty("ali.title");
		Integer property2 = environment.getProperty("ali.number", Integer.class);
		WhiteAlbum property3 = environment.getProperty("test.white",WhiteAlbum.class);
		Integer property4 = environment.getProperty("ali.number", Integer.class, 30);
		//Required 如果获取不到会报错
		String requiredProperty = environment.getRequiredProperty("ali.name");
		
	}
	
	
	/**
	 * 我们将value设置成了WebApplicationContext中的SCOPE_SESSION常量（它的值是session）。
	 * @Scope同时还有一个proxyMode属性，它被设置成了ScopedProxyMode.INTERFACES。
	 * 这个属性解决了将会话或请求作用域的bean注入到单例bean中所遇到的问题。在描述proxyMode属性之前，我们先来看一下proxyMode所解决问题的场景。
	 * 
	 */
	@Bean
	@Scope(value = WebApplicationContext.SCOPE_REQUEST,proxyMode = ScopedProxyMode.INTERFACES)
	public CompactDisc randomBeatlesCD() {
		int choice = (int) Math.floor(Math.random() * 4);
		if (choice == 0) {
			return new SgtPeppers();
		} else if (choice == 1) {
			return new WhiteAlbum();
		} else if (choice == 2) {
			return new HardDaysNight();
		} else {
			return new Revolver();
		}
	}
	

}
